<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加分类-WeAdmin Frame型后台管理系统-WeAdmin 1.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../static/css/font.css">
    <link rel="stylesheet" href="../../static/css/weadmin.css">
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="weadmin-body">

    <form id="form1" class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">节点名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" jq-error="请输入节点名称" placeholder="请输入节点中文名（控制器名称）"
                       autocomplete="off" class="layui-input ">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">节点地址</label>
            <div class="layui-input-block">
                <input type="text" name="title" lay-verify="required|Upper" jq-error="请输入控制器中文名" placeholder="请输入节点地址（控制器英文名（首字母大写））"
                       autocomplete="off" class="layui-input ">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">默认节点</label>
            <div class="layui-input-block">
                <input type="checkbox" name="node[]" title="控制器" lay-skin="primary" checked>
                <input type="checkbox" name="node[]" title="控制器/index" lay-skin="primary" checked>
                <input type="checkbox" name="node[]" title="控制器/tree" lay-skin="primary" checked>
                <input type="checkbox" name="node[]" title="控制器/add" lay-skin="primary" checked>
                <input type="checkbox" name="node[]" title="控制器/edit" lay-skin="primary" checked>
                <input type="checkbox" name="node[]" title="控制器/delete" lay-skin="primary" checked>
                <input type="checkbox" name="node[]" title="控制器/ajax" lay-skin="primary" checked>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="add">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
        <input type="hidden" name="level" value="0"/>
    </form>
</div>
<script src="../../lib/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript">
    layui.extend({
        admin: '{/}../../static/js/admin'
    });
    layui.use(['admin', 'jquery', 'form', 'layer'], function () {
        var admin = layui.admin,
            $ = layui.jquery,
            form = layui.form,
            layer = layui.layer;
        //验证数据
        form.verify({
            Upper: [
                /^[A-Z][a-zA-Z0-9\/_]+$/
                ,'节点地址首字符大写，且不支持特殊符号'
            ]
        });

        $("input[name='title']").on("change",function(){
            //获取input输入的值
            let title=$(this).val();
            let array=['','/index','/tree','/add','/edit','/delete','/ajax'];
            let $boxs=$("input[type='checkbox']");
            $boxs.each(function (index,box) {
                $(box).attr('title',title+array[index]).attr('value',title+array[index]);
            });
            form.render('checkbox');
        });

        //监听提交
        form.on('submit(add)', function (data) {
            let send_json = data.field;
            send_json.level = $("#pid-select option:selected").data('level');
            //发异步，把数据提交给php
            $.ajax({
                url: '/admin/auth_rule/resource',
                method: 'post',
                data: send_json,
                dataType: 'JSON',
                success: function (res) {
                    if (res.code == 0) {
						layer.alert("添加成功", {
							icon: 1
						}, function () {
							// 获得frame索引
                            parent.location.reload();
							var index = parent.layer.getFrameIndex(window.name);
							//关闭当前frame
							parent.layer.close(index);
						});
                    } else {
                        layer.msg(res.msg, {icon: 5});
                    }
                },
                error: function (data) {
                    layer.msg('服务器繁忙', {icon: 5});
                }
            });

            return false;
        });


    });
</script>
</body>

</html>